package com.UnionPayCenter.account.service.impl;

import com.UnionPayCenter.account.entity.Account;
import com.UnionPayCenter.account.entity.ChargeBill;
import com.UnionPayCenter.account.entity.FundsBill;
import com.UnionPayCenter.account.entity.PayBill;
import com.UnionPayCenter.account.entity.dto.ChargeDTO;
import com.UnionPayCenter.account.entity.dto.PayDTO;
import com.UnionPayCenter.account.enums.AccountErrorCodeEnum;
import com.UnionPayCenter.account.enums.AccountTypeEnum;
import com.UnionPayCenter.account.enums.BillDutyEnum;
import com.UnionPayCenter.account.enums.ChargeStatusEnum;
import com.UnionPayCenter.account.enums.FundsBillTypeEnum;
import com.UnionPayCenter.account.enums.PayChannelEnum;
import com.UnionPayCenter.account.enums.PayStatusEnum;
import com.UnionPayCenter.account.manager.ChargeBillManager;
import com.UnionPayCenter.account.manager.PayBillManager;
import com.UnionPayCenter.account.service.AccountService;
import com.UnionPayCenter.account.service.ChargePayService;
import com.UnionPayCenter.account.service.FundsBillService;
import com.UnionPayCenter.account.service.PayService;
import com.UnionPayCenter.distribution.dto.DistributionShareDTO;
import com.UnionPayCenter.distribution.service.DistributionOperatorService;
import com.UnionPayCenter.entity.ShopInfoEntity;
import com.UnionPayCenter.entity.UserEntity;
import com.UnionPayCenter.entity.dto.ChargeResponse;
import com.UnionPayCenter.entity.vo.UserVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.OperationRewardsMapper;
import com.UnionPayCenter.mapper.ShopInfoMapper;
import com.UnionPayCenter.mapper.UserMapper;
import com.UnionPayCenter.operation.entity.Activity;
import com.UnionPayCenter.operation.entity.ActivityType;
import com.UnionPayCenter.operation.entity.OperationRewards;
import com.UnionPayCenter.operation.enums.ActivityOperationTypeEnum;
import com.UnionPayCenter.operation.enums.ActivityRewardTypeEnum;
import com.UnionPayCenter.operation.manager.ActivityManager;
import com.UnionPayCenter.operation.manager.ActivityTypeManager;
import com.UnionPayCenter.service.ICouponService;
import com.UnionPayCenter.service.IUserService;
import com.UnionPayCenter.service.IWechatService;
import com.UnionPayCenter.util.Asserts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ChargePayServiceImpl implements ChargePayService, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private AccountService accountService;

    @Autowired
    private PayBillManager payBillManager;

    @Autowired
    private ChargeBillManager chargeBillManager;

    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Autowired
    private IWechatService wechatService;

    @Autowired
    private IUserService userServie;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ActivityManager operationActivityManager;

    @Autowired
    private ActivityTypeManager activityTypeManager;

    @Autowired
    private OperationRewardsMapper operationRewardsMapper;

    @Autowired
    private ICouponService iCouponService;

    @Autowired
    private DistributionOperatorService distributionOperatorService;

    @Autowired
    private FundsBillService fundsBillService;

    private Map<PayChannelEnum, PayService> payServiceMap = new HashMap<>();

    @PostConstruct
    private void init() {
        Map<String, PayService> payServiceMaps = applicationContext.getBeansOfType(PayService.class);
        for (PayService payService : payServiceMaps.values()) {
            com.UnionPayCenter.account.annotation.PayService service =
                    payService.getClass().getAnnotation(com.UnionPayCenter.account.annotation.PayService.class);
            payServiceMap.put(service.channel(), payService);
        }

    }

    @Override
    @Transactional
    public ChargeResponse charge(ChargeDTO chargeDTO) {

        Asserts.notNull(chargeDTO, "充值信息");
        //Asserts.notBlank(chargeDTO.getOpenId(), "微信openId");
        Asserts.naturalNumber(chargeDTO.getAmount(), "充值金额");
        Asserts.naturalNumber(chargeDTO.getShopId(), "店铺Id");
        Asserts.naturalNumber(chargeDTO.getAccountId(), "账户Id");
        Asserts.notNull(chargeDTO.getPayChannel(), "支付渠道");

        if (chargeDTO.getAmount() == 0) {
            throw new ApiException(ErrorCodeEnum.SW11.code(), "充值金额不能小于等0");
        }

        Account account = accountService.getAccountByAccountId(chargeDTO.getAccountId());
        chargeDTO.setUserId(account.getUserId());

        ChargeBill chargeBill = assembleChargeBill(chargeDTO, account);


        ShopInfoEntity shopInfoEntity = shopInfoMapper.selectByShop(chargeDTO.getShopId());

        chargeBill.setPayeeMerchantId(shopInfoEntity.getMerchantId());


        PayService payService = payServiceMap.get(chargeDTO.getPayChannel());
        if (payService == null) {
            AccountErrorCodeEnum.ACCOUNT_PAY_CHANNEL_NOT_EXIST.exception();
        }
        chargeBill.setShopId(shopInfoEntity.getShopId());
        ChargeResponse chargeResponse = payService.charge(chargeBill, chargeDTO, shopInfoEntity);

        // 充值成功后的奖励
        if (AccountTypeEnum.CHARGE.equals(account.getAccountType())
                && BillDutyEnum.CHARGE.equals(chargeDTO.getBillDuty())) {
            postChargeRewards(chargeDTO, chargeBill, true);
        } else if (AccountTypeEnum.DISTRIBUTION_CHARGE.equals(account.getAccountType()) || AccountTypeEnum.CASH_REWARDS.equals(account.getAccountType())) {
            return null;
        }

        chargeResponse.setAmount(chargeBill.getAmount());

        return chargeResponse;
    }

    @Override
    @Transactional
    public void pay(PayDTO payDTO) {
        Asserts.notNull(payDTO, "支付信息");
        Asserts.naturalNumber(payDTO.getAccountId(), "账户Id");
        Asserts.naturalNumber(payDTO.getAmount(), "实际金额");
        Asserts.naturalNumber(payDTO.getTotalAmount(), "发生金额");
        Asserts.naturalNumber(payDTO.getShopId(), "店铺Id");

        ShopInfoEntity shopInfoEntity = shopInfoMapper.selectByShop(payDTO.getShopId());
        if (shopInfoEntity == null) {
            throw new ApiException(ErrorCodeEnum.TK1010);
        }
        Account account = accountService.getAccountByAccountId(payDTO.getAccountId());
        if (account == null) {
            AccountErrorCodeEnum.ACCOUNT_NOT_EXIST.exception();
        }

        PayService payService = payServiceMap.get(payDTO.getPayChannel());
        if (payService == null) {
            AccountErrorCodeEnum.ACCOUNT_PAY_CHANNEL_NOT_EXIST.exception();
        }


        PayBill payBill = assemblePayBill(payDTO, shopInfoEntity, account);
        UserVo userVo = userServie.getUserInfo(payDTO.getUserId());
        Integer originalBalance = userVo.getTotalBalance();

        payService.pay(payDTO);


        // 分销
        DistributionShareDTO distributionShareDTO = new DistributionShareDTO();
        distributionShareDTO.setPayBill(payBill);
        distributionShareDTO.setBId(Long.valueOf(shopInfoEntity.getShopId()));
        distributionShareDTO.setShopId(Long.valueOf(shopInfoEntity.getShopId()));
        try {
            distributionOperatorService.distributionShare(distributionShareDTO);
        } catch (ApiException e) {
            log.error("distribution api error: {}", e.getMessage());
        } catch (Exception e) {
            log.error("distribution unknow error: {}", e.getMessage());
        }
        if (BillDutyEnum.CHARGE.equals(payDTO.getBillDuty())) {
            return;
        }

        postPayRewards(payBill);

        // 推送支付模板消息
        UserEntity user = userServie.wechatPayGetUser(payDTO.getUserId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String payTime = simpleDateFormat.format(payBill.getCreateTime());
        userVo = userServie.getUserInfo(payDTO.getUserId());
        if (PayChannelEnum.BALANCE_PAY.equals(payDTO.getPayChannel())) {
            //account = accountService.getAccountByUserId(payDTO.getUserId(), AccountTypeEnum.CHARGE);
            wechatService.sendPayMsg(user, payBill.getSn(), payTime, String.valueOf(payBill.getAmount()),
                    String.valueOf(userVo.getTotalBalance()),
                    payBill.getPayChannel().msg(), shopInfoEntity.getShopId());
        }
        payBill.setOriginalBalance(originalBalance);
        payBill.setBalance(userVo.getTotalBalance());
        payBillManager.savePayBill(payBill);
    }

    @Override
    public void updateState(String sn, ChargeStatusEnum state) {
        chargeBillManager.updateState(sn, state);
        ChargeBill chargeBill = chargeBillManager.getBySn(sn, AccountTypeEnum.CHARGE);
        postChargeRewards(null, chargeBill, false);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 支付成功后置处理
     */
    private void postPayRewards(PayBill payBill) {
        Integer shopId = payBill.getShopId();

        UserEntity userEntity = userMapper.selectInfo(payBill.getUserId());
        List<Activity> operationActivities = operationActivityManager.getByShopId(shopId, 1);
        if (CollectionUtils.isEmpty(operationActivities)) {
            return;
        }


        Integer amount = payBill.getTotalAmount();
        Map<Integer, List<Activity>> activityMap = operationActivities.stream()
                .filter(operationActivity -> isMatchCondition(operationActivity, amount))
                .filter(operationActivity -> {
                    ActivityType activityType = activityTypeManager.getById(operationActivity.getActivityTypeId());
                    if (ActivityOperationTypeEnum.BALANCE_PAY.code() != activityType.getOperationType()
                            && ActivityOperationTypeEnum.PAY.code() != activityType.getOperationType()) {
                        return false;
                    }
                    if (activityType.getOperationType() == ActivityOperationTypeEnum.BALANCE_PAY.code()
                            && PayChannelEnum.BALANCE_PAY != payBill.getPayChannel()) {
                        return false;
                    }

                    return activityType.getOperationType() != ActivityOperationTypeEnum.PAY.code()
                            || PayChannelEnum.BALANCE_PAY != payBill.getPayChannel();
                })
                .collect(Collectors.groupingBy(Activity::getRewardsType));

        if (MapUtils.isEmpty(activityMap)) {
            return;
        }

        for (List<Activity> activities : activityMap.values()) {
            Activity matchedActivity = siftNearestActivity(activities, amount);
            if (matchedActivity == null) {
                continue;
            }
            ActivityRewardTypeEnum rewardTypeEnum = ActivityRewardTypeEnum.getEnum(matchedActivity.getRewardsType());
            List<OperationRewards> operationRewards = operationRewardsMapper.selectByActivityId(matchedActivity.getActivityId());
            for (OperationRewards rewards : operationRewards) {
                Integer rewardsQuantity = rewards.getQuantity();
                switch (rewardTypeEnum) {
                    case COUPONS:
                        rewardCoupon(shopId, userEntity.getMobile(), rewards.getCouponsId(), rewardsQuantity,
                                matchedActivity.getEffectiveNextDay());
                        break;
                    case CASH:
                        ChargeDTO chargeDTO = new ChargeDTO();
                        BeanUtils.copyProperties(payBill, chargeDTO);
                        chargeDTO.setChargeDirect(true);
                        chargeRewardsCash(chargeDTO, rewards.getQuantity(), AccountTypeEnum.CASH_REWARDS);
                        break;
                    default:
                        log.error("no matched reward type");
                }
            }
        }
    }

    private boolean isMatchCondition(Activity operationActivity, Integer amount) {
        Integer thresholdStart = operationActivity.getThresholdStart();
        Integer thresholdEnd = operationActivity.getThresholdEnd();

        Date now = new Date();
        if (now.before(operationActivity.getTimeStart()) || now.after(operationActivity.getTimeEnd())) {
            log.debug("activity time mismatch");
            return false;
        }

        if (amount != null && (thresholdStart > amount || thresholdEnd < amount)) {
            log.debug("activity amount mismatch");
            return false;
        }
        return true;
    }

    private Activity siftNearestActivity(List<Activity> activities, Integer amount) {
        int abs = Integer.MAX_VALUE;
        Activity matchedsActivty = null;
        for (Activity operationActivity : activities) {
            int tempAbs = Math.abs(amount - operationActivity.getThresholdStart());
            if (abs > tempAbs) {
                matchedsActivty = operationActivity;
                abs = tempAbs;
            }
        }
        return matchedsActivty;
    }

    /**
     * 送优惠券
     *
     * @param shopId           店铺Id
     * @param mobile           手机号码
     * @param couponId         优惠券Id
     * @param quantity         优惠券数量
     * @param effectiveNextDay 是否次日生效
     */
    private void rewardCoupon(Integer shopId, String mobile, Integer couponId, Integer quantity, Integer effectiveNextDay) {
        try {
            iCouponService.insertUserCoupon(shopId, mobile, couponId, quantity, effectiveNextDay);
        } catch (Exception e) {
            log.error("reward coupon error: [{}]", e.getMessage());
        }

    }

    /**
     * 送优惠券
     *
     * @param shopId           店铺Id
     * @param userId           用户ID
     * @param couponId         优惠券Id
     * @param quantity         优惠券数量
     * @param effectiveNextDay 是否次日生效
     */
    private void rewardCoupon(Integer shopId, Integer userId, Integer couponId, Integer quantity, Integer effectiveNextDay) {
        try {
            iCouponService.insertUserCouponByUserId(shopId, userId, couponId, quantity, effectiveNextDay);
        } catch (Exception e) {
            log.error("reward coupon error: [{}]", e.getMessage(), e);
        }

    }

    public void postRegistRewards(Integer shopId, Integer userId) {
        List<Activity> operationActivities = operationActivityManager.getByShopId(shopId, 1);
        if (CollectionUtils.isEmpty(operationActivities)) {
            return;
        }


        Map<Integer, List<Activity>> activityMap = operationActivities.stream()
                .filter(operationActivity -> isMatchCondition(operationActivity, null))
                .filter(operationActivity -> {
                    ActivityType activityType = activityTypeManager.getById(operationActivity.getActivityTypeId());
                    return ActivityOperationTypeEnum.REGIST.code() == activityType.getOperationType();
                })
                .collect(Collectors.groupingBy(Activity::getRewardsType));

        if (MapUtils.isEmpty(activityMap)) {
            return;
        }

        for (List<Activity> activities : activityMap.values()) {
            if (CollectionUtils.isEmpty(activities)) {
                continue;
            }
            Activity matchedsActivty = activities.get(0);
            ActivityRewardTypeEnum rewardTypeEnum = ActivityRewardTypeEnum.getEnum(matchedsActivty.getRewardsType());
            List<OperationRewards> operationRewards = operationRewardsMapper.selectByActivityId(matchedsActivty.getActivityId());
            for (OperationRewards rewards : operationRewards) {
                Integer rewardsQuantity = rewards.getQuantity();
                if (rewardTypeEnum == ActivityRewardTypeEnum.COUPONS) {
                    rewardCoupon(shopId, userId, rewards.getCouponsId(), rewardsQuantity,
                            matchedsActivty.getEffectiveNextDay());
                } else {
                    log.error("no matched reward type");
                }
            }
        }

    }

    /**
     * 充值成功后置处理
     */
    private void postChargeRewards(ChargeDTO chargeDTO, ChargeBill chargeBill, boolean cash) {

        // 赠送金额的流水和原来的流水保持一致
        if (chargeDTO != null) {
            chargeDTO.setSn(chargeBill.getSn());
        }

        Integer shopId = chargeBill.getShopId();
        UserEntity userEntity = userMapper.selectInfo(chargeBill.getUserId());
        if (userEntity == null) {
            log.error("user is not exist,sn {}", chargeDTO.getSn());
            return;
        }
        List<Activity> operationActivities = operationActivityManager.getByShopId(chargeBill.getShopId(), 1);
        if (CollectionUtils.isEmpty(operationActivities)) {
            return;
        }

        Integer amount = chargeBill.getAmount();
        Map<Integer, List<Activity>> activityMap = operationActivities.stream()
                .filter(operationActivity -> isMatchCondition(operationActivity, amount))
                .filter(operationActivity -> {
                    ActivityType activityType = activityTypeManager.getById(operationActivity.getActivityTypeId());
                    return ActivityOperationTypeEnum.CHARGE.code() == activityType.getOperationType();
                })
                .collect(Collectors.groupingBy(Activity::getRewardsType));

        if (MapUtils.isEmpty(activityMap)) {
            return;
        }

        for (List<Activity> activities : activityMap.values()) {
            Activity matchedActivity = siftNearestActivity(activities, amount);
            if (matchedActivity == null) {
                continue;
            }
            List<OperationRewards> operationRewards = operationRewardsMapper.selectByActivityId(matchedActivity.getActivityId());

            ActivityRewardTypeEnum rewardTypeEnum = ActivityRewardTypeEnum.getEnum(matchedActivity.getRewardsType());
            log.debug("reward type [{}]", rewardTypeEnum);
            for (OperationRewards rewards : operationRewards) {
                Integer rewardsQuantity = rewards.getQuantity();
                switch (rewardTypeEnum) {
                    case COUPONS:
                        if (!cash) {
                            rewardCoupon(shopId, userEntity.getMobile(), rewards.getCouponsId(), rewardsQuantity,
                                    matchedActivity.getEffectiveNextDay());
                        }
                        break;
                    case CASH:
                        if (cash) {
                            chargeRewardsCash(chargeDTO, rewards.getQuantity(), AccountTypeEnum.CASH_REWARDS);
                        }
                        break;
                    case CASH_DISCOUNT:
                        if (cash) {
                            doDiscountAmount(chargeDTO, chargeBill, rewards);
                        }
                        break;
                    default:
                        log.error("no matched reward type");
                }
            }
        }
    }

    /**
     * 充值折扣
     * 1. 修改需要支付的金额 amount
     * 2. 充值优惠差额
     *
     * @param chargeDTO  充值流水请求
     * @param chargeBill 生成的充值流水
     * @param rewards    运营活动奖励
     */
    private void doDiscountAmount(ChargeDTO chargeDTO, ChargeBill chargeBill, OperationRewards rewards) {
        Integer rewardsQuantity = rewards.getQuantity();
        Integer amount = chargeDTO.getAmount();
        Integer payAmount = amount * rewardsQuantity / 100;
        Integer disaccountAmount = amount - payAmount;
        // 重置返回给支付端的金额
        chargeBill.setAmount(payAmount);

        chargeBillManager.updateAmount(chargeBill.getId(), payAmount, amount);

        chargeRewardsCash(chargeDTO, disaccountAmount, AccountTypeEnum.CASH_REWARDS);
    }

    @Override
    public ChargeResponse chargeRewardsCash(ChargeDTO oldChargeDTO, Integer amount, AccountTypeEnum accountType) {

        ChargeDTO chargeDTO = new ChargeDTO();
        BeanUtils.copyProperties(oldChargeDTO, chargeDTO);
        // 如果账户还未建立, 重新初始化一下
        Account rewardsAccount = accountService.getAccountByUserId(chargeDTO.getUserId(), accountType, true);
        if (rewardsAccount == null) {
            Account account = new Account();
            account.setShopId(chargeDTO.getShopId());
            account.setUserId(chargeDTO.getUserId());
            accountService.createAccount(account);
            rewardsAccount = accountService.getAccountByUserId(chargeDTO.getUserId(), accountType);
        }
        chargeDTO.setAmount(amount);
        chargeDTO.setTotalAmount(amount);
        chargeDTO.setAccountId(rewardsAccount.getAccountId());
        chargeDTO.setBillDuty(BillDutyEnum.REWARDS);
        chargeDTO.setPayChannel(PayChannelEnum.BALANCE_PAY);
        return charge(chargeDTO);
    }

    @Override
    public void userImportCharge(Account chargeAccount, Integer amount) {
        ChargeDTO chargeDTO = new ChargeDTO();
        chargeDTO.setAmount(amount);
        chargeDTO.setTotalAmount(amount);
        chargeDTO.setAccountId(chargeAccount.getAccountId());
        chargeDTO.setBillDuty(BillDutyEnum.CHARGE);
        chargeDTO.setPayChannel(PayChannelEnum.IMPORT);
        chargeDTO.setShopId(chargeAccount.getShopId());
        chargeDTO.setUserId(chargeAccount.getUserId());
        chargeDTO.setChargeDirect(true);

        ShopInfoEntity shopInfoEntity = shopInfoMapper.selectByShop(chargeDTO.getShopId());
        ChargeBill chargeBill = assembleChargeBill(chargeDTO, chargeAccount);
        chargeBill.setPayeeMerchantId(shopInfoEntity.getMerchantId());

        PayService payService = payServiceMap.get(chargeDTO.getPayChannel());
        if (payService == null) {
            AccountErrorCodeEnum.ACCOUNT_PAY_CHANNEL_NOT_EXIST.exception();
        }
        chargeBill.setShopId(shopInfoEntity.getShopId());
        payService.charge(chargeBill, chargeDTO, shopInfoEntity);

        FundsBill fundsBill = new FundsBill();
        BeanUtils.copyProperties(chargeBill, fundsBill);
        fundsBill.setId(null);
        fundsBill.setBillType(FundsBillTypeEnum.CHARGE);
        if (fundsBill.getBillDuty() == null) {
            fundsBill.setBillDuty(BillDutyEnum.IMPORT);
        }
        fundsBill.setPayAccountId(chargeBill.getAccountId());
        fundsBill.setPayChannel(chargeBill.getChannelId());
        fundsBill.setState(PayStatusEnum.SUCCESS);
        fundsBillService.saveFundsBill(fundsBill);

    }

    /**
     * 组装支付流水信息
     *
     * @param payDTO         支付请求
     * @param shopInfoEntity 店铺信息
     * @param account        账户信息
     * @return 支付流水信息
     */
    private PayBill assemblePayBill(PayDTO payDTO, ShopInfoEntity shopInfoEntity, Account account) {
        PayBill payBill = new PayBill();
        //UserVo userVo = userServie.getUserInfo(payDTO.getUserId());
        BeanUtils.copyProperties(payDTO, payBill);
        payBill.setShopId(shopInfoEntity.getShopId());
        payBill.setUserId(account.getUserId());
        payBill.setPayAccountId(account.getAccountId());
        payBill.setPayeeMerchantId(shopInfoEntity.getMerchantId());
        payBill.setPayeeAccountType("1");
        //payBill.setOriginalBalance(userVo.getTotalBalance());
        //payBill.setBalance(userVo.getTotalBalance() - payDTO.getAmount());
        //payBill.setOriginalBalance(account.getBalance());
        //payBill.setBalance(account.getBalance() - payDTO.getAmount());
        payBill.setCreateTime(new Date());
        payBill.setUpdateTime(new Date());
        return payBill;
    }

    /**
     * 组装充值流水信息
     *
     * @param chargeDTO 请求
     * @param account   账户
     * @return 充值流水
     */
    private ChargeBill assembleChargeBill(ChargeDTO chargeDTO, Account account) {
        ChargeBill chargeBill = new ChargeBill();
        BeanUtils.copyProperties(chargeDTO, chargeBill);
        chargeBill.setChannelId(chargeDTO.getPayChannel());
        chargeBill.setUserId(account.getUserId());
        chargeBill.setAccountId(account.getAccountId());
        chargeBill.setAccountType(account.getAccountType());
        chargeBill.setBankCardNo(chargeDTO.getOpenId());
        chargeBill.setBankCardName("wx");
        chargeBill.setAmount(chargeDTO.getAmount());
        chargeBill.setTotalAmount(chargeDTO.getTotalAmount());
        chargeBill.setPayeeAccountType("1");
        chargeBill.setBalance(account.getBalance() + chargeDTO.getAmount());
        chargeBill.setOriginalBalance(account.getBalance());
        chargeBill.setPayeeIfkftCust(0);
        return chargeBill;
    }
}
